﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using Sfd.Services;

namespace Sfd.WebApp.Security
{
    public partial class addUserProject : Page
    {
        private readonly ProjectService _projectService = new ProjectService(true);
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                BindDatas();
        }

        private void BindDatas()
        {
            // users
            var allUsers = Membership.GetAllUsers();
            var users = allUsers.Cast<MembershipUser>().Where(user => !Roles.IsUserInRole(user.UserName, "admin")).ToList();

            cbUsers.DataSource = users;
            cbUsers.DataBind();

            if (users.Count > 0)
            {
                cbUsers.SelectedIndex = 0;
                cbUsers_SelectedIndexChanged(null, EventArgs.Empty);
            }
        }

        protected void cbUsers_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbUsers.SelectedIndex == -1)
            {
                return;
            }

            var userId = Guid.Parse(cbUsers.SelectedValue);
            var userProjects = _projectService.GetUserProjects(userId);
            var projects = _projectService.LoadAll();

            var selectedProjects = new List<SelectedProject>();

            foreach (var project in projects)
            {
                selectedProjects.Add(new SelectedProject
                                         {
                                             FriendlyName = project.FriendlyName,
                                             ProjectId = project.Id,
                                             IsSelected = userProjects.Any(prj => prj.Id == project.Id)
                                         });
            }

            gv.DataSource = selectedProjects;
            gv.DataBind();
        }

        protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
        {
            var chk = sender as CheckBox;

            var row = chk.NamingContainer as GridViewRow;
            var projectId = (Guid)gv.DataKeys[row.DataItemIndex].Value;

            var projectName = (row.FindControl("lblProjectName") as Label).Text;

            var userId = Guid.Parse(cbUsers.SelectedValue);
            if (chk.Checked)
            {
                if (_projectService.AddUserToProject(userId, projectId))
                {
                    lblInfos.Text = string.Format("L'utilisateur {0} a été ajouté au projet {1}", cbUsers.SelectedItem, projectName);
                }
                else
                {
                    lblInfos.Text = string.Format("Impossible d'ajouter l'utilisateur {0} au projet {1}", cbUsers.SelectedItem, projectName);
                }
            }
            else
            {
                if (_projectService.RemoveUserFromProject(userId, projectId))
                {
                    lblInfos.Text = string.Format("L'utilisateur {0} a été enlevé du projet {1}", cbUsers.SelectedItem, projectName);
                }
                else
                {
                    lblInfos.Text = string.Format("Impossible d'enlever l'utilisateur {0} du projet {1}", cbUsers.SelectedItem, projectName);
                }                
            }
        }

        public class SelectedProject
        {
            public Guid ProjectId { get; set; }
            public string FriendlyName { get; set; }
            public bool IsSelected { get; set; }
        }

    }
}